/*
 * 网络请求管理类
 */
import 'package:app/config/config.dart';
import 'package:dio/dio.dart';

class DioManager {
  //写一个单例
  //在 Dart 里，带下划线开头的变量是私有变量
  static DioManager _instance;
  int errorCount = 0;

  Dio dio = new Dio();
  DioManager() {
    // Set default configs
    dio.options.headers = {
      "version": GlobalConfig.API_VERSION,
    };
    dio.options.baseUrl = GlobalConfig.BASE_URL;
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
  }
  static DioManager getInstance() {
    if (_instance == null) {
      _instance = DioManager();
    }
    // 调试模式下开启请求日志打印
    if (GlobalConfig.isDebug) {
      _instance.dio.interceptors.add(LogInterceptor(
          request: false, // 不打印请求
          requestBody: true, // 打印请求体
          responseHeader: false, // 不打印响应头
          responseBody: true)); // 打印响应体
    }
    return _instance;
  }
}
